module.exports = function (mainModule) {
  mainModule.controller('mainCtrl',
    function ($rootScope,
              $scope,
              $timeout,
              $ionicScrollDelegate,
              $location,
              $log,
              $state,
              $native,
              Config,
              Dom,
              mainFactory) {

      $scope.$watch('$viewContentLoaded', function () {
        $scope.vc.refresh();
        //@TODO: 隐藏下载弹出浮层
        if($rootScope.args.autoload === '1'){
          var node = document.getElementsByClassName('fz_download')[0];
          Dom.removeClass(node, 'show');
        }
      });

      $scope.$on('$ionicView.afterEnter', function() {
        $native.recordEvent(Config.pressEventKey);
      });


      // 初始化设置加载状态
      $scope.vo = {
        title:'新闻',
        myStocks:$rootScope.args.s,
        tpl:'list.html'
      };

      //页面状态
      $scope.status = Config.status.init;

      $scope.vc = {
        /* 首次加载或下拉刷新调用 */
        refresh:function (type) {
          if(!type){
            $scope.status = Config.status.loading;
          }
          var promise = mainFactory.refresh($scope.vo.myStocks);
          promise.then(function (response) {
            $scope.vo.stocks = [];
            $scope.vo.stocks = response;
            if($scope.vo.stocks.length === 0){
              $scope.status = Config.status.noData;
            }else{
              $scope.status = Config.status.success;
            }
          }, function (error) {
            $scope.status = Config.status.timeout;
          }).finally(function() {
            $scope.$broadcast('scroll.refreshComplete');
            var webCnt = document.getElementById('webCnt');
            $timeout(function () {
              if(ionic.Platform.isIOS() && $rootScope.args.autoload == '1'){
                $native.setInforViewHeight(parseFloat(webCnt.offsetHeight));
              }
            },100);
          });
        },
        /* 上拉加载更多 */
        loadMore:function () {
          var promise = mainFactory.loadMore($scope.vo.myStocks);
          promise.then(function (response) {
            $scope.vo.stocks = response;
            if($scope.vo.stocks.length === 0){
              $scope.status = Config.status.noData;
            }else{
              $scope.status = Config.status.success;
            }
            var webCnt = document.getElementById('webCnt');
            $timeout(function () {
              if(ionic.Platform.isIOS() && $rootScope.args.autoload == '1'){
                $native.setInforViewHeight(parseFloat(webCnt.offsetHeight));
              }
            },100);
          }, function (error) {
            $scope.status = Config.status.timeout;
          }).finally(function() {
            $scope.$broadcast('scroll.infiniteScrollComplete');
          });
        },
        hasNextPage:function () {
          return mainFactory.hasNextPage();
        },
        doSwiper:function () {
          var contentScroll = $ionicScrollDelegate.$getByHandle('pressScroll');
          var top = parseInt(contentScroll.getScrollPosition().top);
          // 显示和隐藏返回顶部弹层
          var goTopBtn = document.getElementById('selfGoTop');
          if(goTopBtn){
            if(top > 0){
              goTopBtn.setAttribute('style', 'opacity: 1');
            }else{
              goTopBtn.setAttribute('style', 'opacity: 0');
            }
          }
        },
        goTop:function () {
          // 显示和隐藏返回顶部弹层
          var goTopBtn = document.getElementById('selfGoTop');
          goTopBtn.setAttribute('style', 'opacity: 0');
          var contentScroll = $ionicScrollDelegate.$getByHandle('pressScroll');
          contentScroll.scrollTop(true);
        },
        //点击改变列表背景色
        onTouch:function($event){
          if($rootScope.args.pattern == 0){
            $event.target.parentNode.style.backgroundColor = '#2C3338';
          }else{
            $event.target.parentNode.style.backgroundColor = '#EBEFF2';
          }
        },
        //松开背景色消失
        onRelease:function($event){
          $event.target.parentNode.style.backgroundColor = '';
        },
        goContent:function ($event, info, describe) {
          var url = '#/content/'+info.infoCode+ '?s='+info.stock[0].code
            +'&pattern=' + $rootScope.args.pattern
            + '&appId=' + $rootScope.args.appId ;
          console.log(url)
          // 0.0 页面跳转
          if(typeof(nativeObject) !== 'undefined'){
            var baseURL = $location.protocol() + '://' + $location.host() ;
            url =  (baseURL + '/api/newinfo/www/press.html?' + url);
            var patternBol = ($scope.vo.pattern == 0 ? 'true' : 'false');
            var params = JSON.stringify({url:url, title:$scope.vo.title,describe:describe, supportBlack: patternBol});
            $log.warn(params);
            openNewDetail(params);
          }else{
            $state.go('content', {infoCode:info.infoCode, pattern:$rootScope.args.pattern, appId:$rootScope.args.appId});
          }
          // 1.0 改变阅读颜色
          $event.target.style.color = '#818B91';
        }
      };
  });
};
